# ##############-------------------------------------第4章列表###################

# #-----------------------------4.1 列表数据类型
# # [1,2,3]
# # ['bat','cat','rat','elephant']
# # ['hello',3.1415,True,None,42]
# # spam = ['bat','cat','rat','elephant']
# # print(spam)


# ######4.1.1 索引取得列表中的单个值
# # test = ['cat','bat','rat','elephant']
# # print(test[0])
# # print(test[2])
# # print(test[3])
# # print('Hello ' + test[3])

# # dualtest = [['cat','rat'],[10,20,30,40]]
# # print(dualtest[0])
# # print(dualtest[0][1])

# #######4.1.2负数索引
# spam = ['cat','bat','rat','elephant']
# # print(spam[-1])

# #######4.1.3 利用切片取得子列表
# # print(spam[1:3])
# # print(spam[0:-1])
# # print(spam[:4])

# ######4.1.4 用len()函数取得列表的长度
# print(len(spam))
# #######4.1.5用索引改变列表的值
# spam[1] = 'Hello'
# print(spam)
# spam[-1] = 12345
# print(spam)
# #######4.1.6列表连接和列表复制
# list = spam + [1,2,3]+['A','B','C']
# print(list)
# #######4.1.7用del语句从列表中删除值
# del list[8]
# print(list)

#-----------------------------------------------4.2 使用列表

# catnames = []

# while True:
#     print('Enter the cat name: '+str(len(catnames)+1)+' or Enter nothing to stop')
#     name = input()
#     if name == '':
#         break
#     catnames = catnames + [name]
# print(catnames)

# catnames = []

# while True:
#     print('Enter the cat name: '+str(len(catnames)+1)+' or Enter nothing to stop')
#     name = input()
#     if name == '':
#         break
#     catnames = catnames + [name]
# print('The cat names are: ')
# for name in catnames:
#     print(' '+name)

#4.2.1 列表用于循环
# supplies = ['pen','staplers','flame-flowers','blinders']
# for i in range(len(supplies)):
#     print('Index ' + str(i) + ' in supplies is ' + supplies[i])

#4.2.2 in and not in操作符
# print('howdy' in ['hello','hi','howdy','heyas'])
# spam = ['hello','hi','howdy','heyas']
# print('cat' not in spam)

# mypets = ['djl','fyk','ljd']
# print('Enter a pet name:')
# name = input()
# if name not in mypets:
#     print('I do not have a pet named '+name)
# else:
#     print(name+' is my pet')

#4.2.3多重赋值技巧
# cat = ['fat','black','loud']
# size,color,disposition=cat
# print(size)

#4.2.4 enumerate()函数与列表一起使用
# supplies = ['pens','staplers','flamethrowers','binders']
# for index,item in enumerate(supplies):
#     print('Index '+str(index)+' in supplies is '+item)

#4.2.5 random.choice()和random.shuffle
# import random
# pets = ['Dog','Cat','Moose']
# print(random.choice(pets))
# random.shuffle(pets)
# print(pets)

#4.3 增强的赋值操作——Python中赋值操作符可以作用于字符串和列表


#---------------------------------4.4 方法——只在一个值上调用
#---------------------------------4.4.1 用index()方法在列表中查找值
# spam = ['hello','heyas','hi','howdy','heyas']
# print(spam.index('hello'))
# print(spam.index('heyas'))
# print(spam.index('he'))
#---------------------------------4.4.2 用append()方法和insert()方法在列表中添加值
# spam = ['cat','dog','monkey']
# spam.append('moose')
# spam.insert(1,'panda')
# print(spam)
#---------------------------------4.4.3 用remove()方法和在列表中删除值
# spam.remove('dog')
# print(spam)
#---------------------------------4.4.4 用sort()方法将列表中的值排序
# spam = ['Kff','cqz','Mxy','hzl']
# spam.sort()
# print(spam)
# spam.sort(key=str.lower)
# print(spam)
#---------------------------------4.4.5 用reverse()方法反转列表中的值
# spam.reverse()
# print(spam)

#--------------------------------4.5 神奇8球与列表
# import random
# message = ['It is certain',
#            'It is decidely so',
#            'Reply hazy try again',
#            'Ask again later',
#            'Concentrate and try again',
#            'My reply is no',
#            'Outlook no so good',
#            'Very doubtful']
# print(random.choice(message))
# print(message[random.randint(0,len(message)-1)])

#--------------------------------4.6 序列数据类型：列表，字符串，range()返回的范围对象，元组
# name = 'Kefafu'
# print(name[0])
# print(name[-2])
# print(name[0:4])
# print('cqz' in name)
# for i in name:
#     print('***'+i+'***')

#--------------------------------4.6.1 可变和不可变数据类型：列表，字符串、元组
#--------------------------------4.6.2 元组数据类型
# print(type(('hello'))) #字符串
# print(type(('hello',))) #元组
#--------------------------------4.6.3 用list()和tuple()函数来转换类型
# print(type(tuple(['cat','dog',5])))
# print(type(list(['cat','dog',5])))
# print(type(list('hello')))
# print((list('hello')))


#--------------------------------4.7 引用
#变量的存储-变量存储的是对计算机内存位置的引用，变量值不可变
# spam = 42
# cheese = spam
# print(id(spam))
# print(id(cheese))
# spam = 100
# print(spam)
# print(cheese)
# print(id(spam))
# print(id(cheese))
#列表的存储——列表值可变
# spam = [0,1]
# cheese = spam
# print(id(spam))
# print(id(cheese))
# cheese[1] = 'Hello'
# print(spam)
# print(cheese)
# print(id(spam))
# print(id(cheese))

#-------------------------------4.7.1 标识和id()函数
#变量值不可变，修改变量值销毁了之前的内存位置，创建了新位置
# bacon = 'Hello'
# print(id(bacon))
# bacon += 'World'
# print(id(bacon))
#列表值可变，且存储位置不变-“就地修改”
# eggs = ['cat','dog']
# print(id(eggs))
# eggs.append('elephant') #append改变现有的列表
# print(id(eggs))
# eggs =[0,1] #新列表-新位置
# print(id(eggs))

#--------------------------------4.7.2 传递引用
# def eggs(someParameter):
#     someParameter.append('World')
# spam = [1,2,3]
# print(id(spam))
# eggs(spam)
# print(spam)
# print(id(spam))
#--------------------------------4.7.3copy()模块的copy()和deepcopy()函数
#copy()复制列表值，但创建一个新的存储位置，deepcopy()复制含有列表的列表
import copy
spam = ['A','B','C','D']
print(id(spam))
cheese = copy.copy(spam)
print(id(cheese))
print(cheese)
cheese[1] = 10086
print(spam)
print(cheese)
print(id(spam))
print(id(cheese))